/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : main.c                                         	      *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-07-8                                             *
 *  $Hard Version     : KF32A156-MINI-EVB_V1.2                                *
 *  $Description$     : Flash the LED0 every 500ms on the KF32A156 Mini EVB,  *
 * 						and press the key to flip another LED1				  *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyright protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-07-08 00.02.00 FAE Group     Version 2.0 update                      *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
**                      		Include Files                                **
******************************************************************************/
#include "system_init.h"

/******************************************************************************
**                      	Private variables                                **
******************************************************************************/
static volatile uint32_t TimingDelay;

/*******************************************************************************
**                     		   Global Functions 		             	      **
*******************************************************************************/
/**
 *  @brief :Initialize the different GPIO ports
 * 			PC0 ------------- S2
 * 			PD12 ------------ LED1
 * 			PH3 ------------- LED2
 *  @param in :None
 *  @param out :None 
 *  @retval :None
 */
void BoardGpioInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	/* Configure PD12 LED1 output */
	GPIO_InitStructure.m_Mode = GPIO_MODE_OUT;
	GPIO_InitStructure.m_OpenDrain = GPIO_POD_PP;
	GPIO_InitStructure.m_PullDown = GPIO_NOPULL;
	GPIO_InitStructure.m_PullUp = GPIO_NOPULL;
	GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_6;
	GPIO_Configuration(GPIOC_SFR , &GPIO_InitStructure);
	
	/* Configure PC5 LED2 output */
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_5;
	GPIO_Configuration(GPIOC_SFR , &GPIO_InitStructure);
	
	 /* Configure PC0 user key input */
	GPIO_InitStructure.m_Mode = GPIO_MODE_IN;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_0;
	GPIO_Configuration(GPIOC_SFR , &GPIO_InitStructure);
}

/*******************************************************************************
**                     			Main Functions 		             	     	  **
*******************************************************************************/
/**
 *  @brief :Main program
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
int main()
{
	/*initialize system clock *********/
	SystemInit(SYSCLK_FREQ);
	/*T=1/48M*48=1us,48M:SCLK*/
	systick_delay_init(SYSCLK_FREQ);
	/* Initialize the user IOs */
	BoardGpioInit();
	while(1)
	{
		/* Toggle LED1 */
		if (++TimingDelay >= 10)
		{
			TimingDelay = 0;
			GPIO_Toggle_Output_Data_Config(GPIOC_SFR, GPIO_PIN_MASK_6);
		}
		/* Turn ON/OFF the dedicate LED2 by user key */
		if (GPIO_Read_Input_Data_Bit(GPIOC_SFR, GPIO_PIN_MASK_0) == Bit_RESET)
		{
			GPIO_Set_Output_Data_Bits(GPIOC_SFR, GPIO_PIN_MASK_5, Bit_SET);
		}
		else
		{
			GPIO_Set_Output_Data_Bits(GPIOC_SFR, GPIO_PIN_MASK_5, Bit_RESET);
		}
		systick_delay_ms(50);
	}		
}

/**
 *  @brief : Reports the name of the source file and the source line number
 *           where the assert_param error has occurred.
 *  @param in :	file pointer to the source file name
 * 	@param in :	line assert_param error line source number
 *  @param out :None
 *  @retval :None
 */
void check_failed(uint8_t* File, uint32_t Line)
{
	/* User can add his own implementation to report the file name and line number,
		ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while(1)
	{
		;
	}
};
